import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/forms/forms.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:reactive_forms/reactive_forms.dart';

class ResetStudentPasswordReactiveDialog extends StatelessWidget {
  ResetStudentPasswordReactiveDialog({
    Key? key,
    required this.userName,
  }) : super(key: key);
  final String userName;
  @override
  Widget build(BuildContext context) {
    UserController _usercontroller = Get.find();
    ResetPasswordFormModel formModel = ResetPasswordFormModel();
    var form = formModel.form;

    return AlertDialog(
      title: Text('Reset Password'.tr),
      titleTextStyle: TextStyle(
          fontWeight: FontWeight.bold,
          letterSpacing: 1,
          color: Get.isDarkMode ? Color(0xffcccccc) : Colors.black,
          fontSize: 20),
      backgroundColor: Get.isDarkMode
          ? (Color(0xff333333))
          : Color.fromARGB(255, 234, 241, 238),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(20))),
      content: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          width: 400,
          height: 208,
          child: ReactiveForm(
            formGroup: form,
            child: Column(children: [
              Expanded(
                  child: ReactiveTextField(
                formControlName: 'password',
                obscureText: true,
                decoration: InputDecoration(
                  labelText: 'Password'.tr,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8.0)),
                ),
                validationMessages: {
                  ValidationMessage.min: (_) =>
                      'Password must be at least 6 characters in length'.tr
                },
              )),
              Expanded(
                  child: ReactiveTextField(
                formControlName: 'confirmpassword',
                obscureText: true,
                decoration: InputDecoration(
                  labelText: 'Confirm Password'.tr,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(8.0)),
                  //hintText: 'Confirm Password'.tr
                ),
                validationMessages: {
                  ValidationMessage.mustMatch: (_) =>
                      'Confirm Password mismatch!'.tr,
                },
              )),
              SizedBox(
                height: 12,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SizedBox(
                    width: 50,
                  ),
                  MaterialButton(
                      color: Get.isDarkMode
                          ? (Colors.white)
                          : Theme.of(context).primaryColorDark,
                      textColor: Get.isDarkMode
                          ? (Colors.black)
                          : Theme.of(context).primaryColorLight,
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text('Cancel'.tr)),
                  SizedBox(
                    width: 50,
                  ),
                  ReactiveFormConsumer(
                    builder: (context, form, child) {
                      return MaterialButton(
                          color: Get.isDarkMode
                              ? (Colors.white)
                              : Theme.of(context).primaryColorDark,
                          textColor: Get.isDarkMode
                              ? (Colors.black)
                              : Theme.of(context).primaryColorLight,
                          onPressed: () {
                            if (form.valid) {
                              var pwd = form.control('confirmpassword').value;
                              _usercontroller
                                  .resetStudentPassword(this.userName, pwd)
                                  .then((statusMessage) {
                                if (statusMessage.status == 1000) {
                                  showSnackMsg('Password reset successfully');
                                  // Get.snackbar(
                                  //   'Message'.tr,
                                  //   'Password reset successfully'.tr,
                                  //   snackPosition: SnackPosition.BOTTOM,
                                  //   backgroundColor:
                                  //       Theme.of(context).primaryColor,
                                  //   colorText:
                                  //       Theme.of(context).primaryColorLight,
                                  // );
                                  Future.delayed(Duration(milliseconds: 1000),
                                      (() {
                                    Navigator.of(context).pop();
                                  }));
                                } else {
                                  showSnackMsg('Failed to reset password');
                                  // Get.snackbar(
                                  //   'Message'.tr,
                                  //   'Failed to reset password'.tr,
                                  //   snackPosition: SnackPosition.BOTTOM,
                                  //   backgroundColor:
                                  //       Theme.of(context).primaryColor,
                                  //   colorText:
                                  //       Theme.of(context).primaryColorLight,
                                  // );
                                }
                              });
                            } else {
                              form.markAllAsTouched();
                            }
                          },
                          child: Text('Submit'.tr));
                    },
                  ),
                ],
              ),
            ]),
          ),
        ),
      ),
    );
  }
}
